//
//  ZKTransitionAnimator.m
//  朱凯的工具箱
//
//  Created by pro on 17/5/12.
//  Copyright © 2017年 zhukai. All rights reserved.
//

#import "KZTransitionAnimator.h"


@interface KZTransitionAnimator ()

@property (nonatomic, strong) KZTransitionAnimatedConfig    *toTransition;      // push
@property (nonatomic, strong) KZTransitionAnimatedConfig    *backTransition;    // pop

@property (nonatomic, strong) KZTransitionInteractiveConfig *toInteractive;     //
@property (nonatomic, strong) KZTransitionInteractiveConfig *backInteractive;   //

@end

@implementation KZTransitionAnimator

- (instancetype)init {
    
    if (self = [super init]) {
        
        self.toDuration = self.backDuration = 0.5;
    }
    return self;
}

#pragma mark - 懒加载
- (KZTransitionAnimatedConfig *)toTransition {
    
    if (!_toTransition) {
        
        _toTransition = [[KZTransitionAnimatedConfig alloc] initWithDuration:self.toDuration config:^(id<UIViewControllerContextTransitioning> context) {
            
            [self setToTransitionAnimationConfig:context];
        }];
    }
    return _toTransition;
}
- (KZTransitionAnimatedConfig *)backTransition {
    
    if (!_backTransition) {
        
        _backTransition = [[KZTransitionAnimatedConfig alloc] initWithDuration:self.backDuration config:^(id<UIViewControllerContextTransitioning> context) {
            
            [self setBackTransitionAnimationConfig:context];
        }];
    }
    return _backTransition;
}

#pragma mark - 动画实现config
- (void)setToTransitionAnimationConfig:(id<UIViewControllerContextTransitioning>)transitionContext {
    
    /*
     在子类中实现，否则执行默认转场动画
     */
    
    // 设置默认行为
    [self defaultAnimationForToTransition:transitionContext];
}
- (void)setBackTransitionAnimationConfig:(id<UIViewControllerContextTransitioning>)transitionContext {
    
    /*
     在子类中实现
     */
    
    // 设置默认行为
    [self defaultAnimationForBackTransition:transitionContext];
    
}
- (void)defaultAnimationForToTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    
    UIView *containerView = [transitionContext containerView];
    for (UIView *subview in containerView.subviews) {
        NSLog(@"start_subview:%p", subview);
    }
    
    // 要前往的试图控制器
    UIView *toView = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view;
    NSLog(@"to_view:%p,frame:%@", toView, NSStringFromCGRect(toView.frame));
    // 当前视图控制器
    UIView *fromView = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view;
    NSLog(@"from_view:%p,frame:%@", fromView, NSStringFromCGRect(fromView.frame));

    // 给容器添加视图
    [containerView addSubview:toView];
    [containerView sendSubviewToBack:fromView];
    [containerView bringSubviewToFront:toView];
    
    // 黑色半透明视图
    UIView *mask = [[UIView alloc] initWithFrame:fromView.frame];
    mask.backgroundColor = [UIColor blackColor];
    [containerView insertSubview:mask aboveSubview:fromView];

    // 动画前的视图配置
    mask.alpha = 0.0;
    toView.transform = CGAffineTransformMakeTranslation(SCREEN_WIDTH, 0);
    
    [UIView animateWithDuration:self.toDuration
                     animations:^{
                         
                         mask.alpha = 0.4;
                         toView.transform = CGAffineTransformIdentity;
                         fromView.frame = CGRectOffset(containerView.bounds, -SCREEN_WIDTH / 4, 0);
                         mask.frame = fromView.frame;
                         
                     }
                     completion:^(BOOL finished) {

                         [mask removeFromSuperview];
                         
                         [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
                         
                     }];

}
- (void)defaultAnimationForBackTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    
    // 容器中本身包含一个fromView，其他的要自己添加
    UIView *containerView = [transitionContext containerView];
    for (UIView *subview in containerView.subviews) {
        NSLog(@"start_subview:%p", subview);
    }
    
    // 要前往的试图控制器
    UIView *toView = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view;
    NSLog(@"to_view:%p,frame:%@", toView, NSStringFromCGRect(toView.frame));
    // 当前视图控制器
    UIView *fromView = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view;
    NSLog(@"from_view:%p,frame:%@", fromView, NSStringFromCGRect(fromView.frame));
    
    
    // 给容器添加视图
    [containerView addSubview:toView];
    [containerView sendSubviewToBack:toView];
    [containerView bringSubviewToFront:fromView];
    
    // 黑色半透明视图
    UIView *mask = [[UIView alloc] initWithFrame:fromView.frame];
    mask.backgroundColor = [UIColor blackColor];
    [containerView insertSubview:mask aboveSubview:toView];

    
    toView.frame = CGRectOffset(containerView.bounds, -SCREEN_WIDTH / 4, 0);
    mask.alpha = 0.4;
    mask.frame = toView.frame;

    [UIView animateWithDuration:self.backDuration
                     animations:^{
                         
                         toView.frame = containerView.bounds;
                         mask.alpha = 0.0;
                         mask.frame = toView.frame;
                         fromView.transform = CGAffineTransformMakeTranslation(SCREEN_WIDTH, 0);
                     }
                     completion:^(BOOL finished) {
                         
                         [mask removeFromSuperview];
                         
                         [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
                     }];
    
}
#pragma mark view controller transition delegate
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    
    return self.toTransition;
    
}

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    
    return self.backTransition;
}

- (nullable id <UIViewControllerInteractiveTransitioning>)interactionControllerForPresentation:(id <UIViewControllerAnimatedTransitioning>)animator {
    
    return self.toInteractive.interation ? self.toInteractive : nil;
}

- (nullable id <UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id <UIViewControllerAnimatedTransitioning>)animator {
    
    return self.backInteractive.interation ? self.backInteractive : nil;
}


@end
